/**
 * This class represents a single factory providing a single RPC channel.
 */
package org.hackystat.ui.sensordatabrowser.client;

import org.hackystat.ui.sensordatabrowser.client.common.RPCRemoteService;
import org.hackystat.ui.sensordatabrowser.client.common.RPCRemoteServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

/**
 * This class represents a single factory providing a single RPC channel.
 * @author David J. Nickles
 *
 */
public class RPCServiceFactory {
  
  private static RPCServiceFactory singular;

  /** {@literal} SERVLET_PATH context path for servlet. */
  private static final String SERVLET_PATH = "sensordatabrowser";
  /** {@literal} PRCERRORNAME name for RPC errors. */  
  public static final String PRCERRORNAME = "Error on Version RPC"; 

  /* RPC procedure variables */
  private String serviceURL;
  private RPCRemoteServiceAsync service;
  private ServiceDefTarget endpoint;

  /**
   * This method instantiates the special object needed for the RPC call to the servlet.
   */
  private void initRPC() {
    this.service = (RPCRemoteServiceAsync) GWT.create(RPCRemoteService.class);
    this.endpoint = (ServiceDefTarget) this.service;
    this.serviceURL = GWT.getModuleBaseURL() + SERVLET_PATH;
    this.endpoint.setServiceEntryPoint(this.serviceURL);
  }
  
  /**
   * Construct all the Remote Procedure Call Services.
   */
  private RPCServiceFactory() {
    initRPC();
  }
  
  /**
   * Get a reference to the single RPCServiceFactory.
   * @return a singular reference to a RPCServiceFactory that this class maintains.
   */
  public static RPCServiceFactory getInstance() {
    if (singular == null) {
      return (singular = new RPCServiceFactory());
    } 
    else {
      return singular;
    }
  }
  
  /**
   * Get a reference to the single RPC service.
   * @return a singular reference to a RPCServiceFactory that this class maintains.
   */
  public RPCRemoteServiceAsync getService() {
    return this.service;
  }
}
